﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LoginModule
{
    public class DangKy
    {

        // Dang ky thanh lap don vi 

        private AccountModuleDataContext dangKyThanhLap = new AccountModuleDataContext();

        public int dangKyThanhLapDonVi(SOQUANLYDONVI soQuanLyDonVi) 
        {
            int themMoi = 0;
            try
            {
                dangKyThanhLap.SOQUANLYDONVIs.InsertOnSubmit(soQuanLyDonVi);
                dangKyThanhLap.SubmitChanges();
                themMoi = 1;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            return themMoi;
        }

        // Dang ky tai khoan

        public int dangKyTaiKhoan(DANGKYSUDUNG dangKySuDung)
        {
            int them = 0;
            try
            {
                dangKyThanhLap.DANGKYSUDUNGs.InsertOnSubmit(dangKySuDung);
                dangKyThanhLap.SubmitChanges();
                them = 1;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return them;
        }

        // Lay thong tin don vi
        public SOQUANLYDONVI thongTinDonVi(Int32 id)
        {
            return dangKyThanhLap.SOQUANLYDONVIs.SingleOrDefault(p => p.MASO == id);
        }
        // Lay thong tin don vi chi tiet
        public List<SOQUANLYDONVI> thongTinDonViChiTiet(Int32 id)
        {
            return dangKyThanhLap.SOQUANLYDONVIs.Where(p => p.MACAPTRUCTHUOC == id).ToList();
        }

        public string dangNhapTaiKhoan(string tentaikhoan, string matkhau)
        {
            IList<DANGKYSUDUNG> dangKySuDung = dangKyThanhLap.DANGKYSUDUNGs.Where(p => p.TENTAIKHOAN == tentaikhoan && p.MATKHAU == matkhau).ToList();

            if (dangKySuDung.Count == 0)
            {
                return "B";
            }
            else
            {
                return "A";
            }
        }

        public void themDonViNganh(SOQUANLYDONVI soquanlyDonVi, DANHMUCDONVINGANH danhMucDonViNganh)
        {
            try
            {
                dangKyThanhLap.SOQUANLYDONVIs.InsertOnSubmit(soquanlyDonVi);
                dangKyThanhLap.DANHMUCDONVINGANHs.InsertOnSubmit(danhMucDonViNganh);
                dangKyThanhLap.SubmitChanges();
            }
            catch(Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        
    }
}
